package TestDemo2;

import TestDemo1.Test;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-28
 * Time: 17:15
 */
public class Test2 {
    private int[] arr1;//缓冲区1
    private int[] arr2;//缓冲区2
    private int arr1Size;
    private int arr2Size;
    public Test2() {
        this.arr1=new int[10];
        this.arr2=new int[5];
        this.arr1Size=0;
        this.arr2Size=0;
        RandomThread randomThread=new RandomThread();//生成随机数的线程
        ResolveThread resolveThread=new ResolveThread();//分解因数线程
        PrintThread printThread=new PrintThread();//打印线程
        new Thread(randomThread,"随机线程").start();
        new Thread(resolveThread,"分解线程1").start();
        new Thread(resolveThread,"分解线程2").start();
        new Thread(resolveThread,"分解线程3").start();
        new Thread(printThread,"打印线程").start();
    }

    private class RandomThread implements Runnable{
        Random random=new Random();
        @Override
        public void run() {
           while (true){
               try {
                   if(arr1Size>=10){
                       Thread.sleep(1000);
                       break;
                   }
                   arr1[arr1Size++]=random.nextInt(2^31);
                   System.out.println(Thread.currentThread().getName()+" 成功生成了一个随机数");
               }catch (Exception e){
                   e.printStackTrace();
               }

           }

        }
    }
    private class ResolveThread implements Runnable{
        Random random = new Random();
        Object lock=new Object();
        @Override
        public void run() {
            while(true){
                synchronized (lock){
                    try {
                        if(arr1Size==0){
                            Thread.sleep(1000);
                        }if(arr2Size>=5){
                            break;
                        }
                        else{
                            int index=random.nextInt(10)%10;
                            int m=arr1[index];
                            int n=m;
                            arr2[arr2Size++]=n;
                            for (int i = 2; i <=Math.sqrt(m)&&arr2Size<=5 ; i++) {
                                while(n%i==0&&arr2Size<=5){
                                    arr2[arr2Size++]=i;
                                    n/=i;
                                }
                                if(n>Math.sqrt(m)){
                                    arr2[arr2Size++]=m;
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    private class PrintThread implements Runnable{

        @Override
        public void run() {
            try {
                if(arr2Size==0){
                    Thread.sleep(1000);

                }else{
                    for (int i = 0; i < arr2Size; i++) {
                        System.out.print(arr2[i]);
                        if(i==0){
                            System.out.print("=");
                        }
                        if(i!=0&&i!=arr2Size-1){
                            System.out.print("*");
                        }
                    }
                    System.out.println();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Test2 test=new Test2();
    }
}
